package com.djsearch.util;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.content.Context;
import android.graphics.Color;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.method.LinkMovementMethod;
import android.text.style.ForegroundColorSpan;
import android.widget.TextView;


public class LinkableUntil {
	public static void setLinkableText(Context context,TextView view,String StrText,boolean IsCanClick,String contentUrl){
		
		SpannableString sp = new SpannableString(StrText);
		CheckURLClickString(context,sp,contentUrl);	
	}
	private static String  CheckURLClickString(Context context,SpannableString spstr,String contentUrl) {
		Matcher localMatcher =  Pattern.compile("http[s]*://[[[^/:]&&[a-zA-Z_0-9]]\\.]+(:\\d+)?" +
				"(/[a-zA-Z_0-9]+)*(/[a-zA-Z_0-9]*([a-zA-Z_0-9]+\\.[a-zA-Z_0-9]+)*)?(\\?(&?[a-zA-Z_0-9]+=" +
				"[%[a-zA-Z_0-9]-]*)*)*(#[[a-zA-Z_0-9]|-]+)?").matcher(spstr);
		String text=""	;	
		if(localMatcher.find()) {
		   text = spstr.subSequence(localMatcher.start(), localMatcher.end()).toString();
						
//			spstr.setSpan(new UrlClickSpan(context,text,docid), localMatcher.start(), localMatcher.end(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
		    
		}
		return text;
	}
	
	/**
	 * 处理时间
	 * @param getTime
	 * @return
	 */
	public static String getTime(String getTime){
		 String day_one=getTime.substring(3, 5);
		 String hour_one=getTime.substring(6, 8);
		 String min_one=getTime.substring(9,11);
		 SimpleDateFormat   formatter   =   new   SimpleDateFormat   ("MM-dd HH:mm     ");      
		 Date   curDate   =   new   Date(System.currentTimeMillis());//获取当前时间      
		 String   str   =   formatter.format(curDate);  
		 System.out.println("当前时间是："+str);
		 
		 String day=str.substring(3, 5);
		 String hour=str.substring(6, 8);
		 String min=str.substring(9,11);

		System.out.println("day"+day+"hour"+hour+"min"+min);
		
		if(day_one.equals(day)&&hour_one.equals(hour)){
			int minute_one=Integer.parseInt(min_one);
           int minute=Integer.parseInt(min);
           int x=Math.abs(minute_one-minute);
          if(x==0){
        	  return "刚刚" ;
          }else{
        	  return (x+"分钟前");
          }
           
		}else if(day_one.equals(day)&&!hour_one.equals(hour)){
			int hou_one=Integer.parseInt(hour_one);
           int hou=Integer.parseInt(hour);
           int x=Math.abs(hou_one-hou);
           return (x+"小时前");
		}
		else{
			return getTime;
		}
	}
}
